# 4.6 Aop tool

# 1. Aop

# 1.1 get(...)

Aop.get(...) allows the creation of objects at any time and space and performs dependency injection on them. For instance:

Service service = Aop.get(Service.class);
1

The above code creates a Service object. If the Service has interceptors configured with @Before, they will be activated. If the properties in Service are annotated with @Inject, they will be injected with dependent objects.

# 1.2 inject(...)

Aop.inject(...) can inject target objects at any time and space. This method, compared to Aop.get(...), lacks the object creation functionality:

Service service = new Service(...);
Aop.inject(service);
1
2

The above code will inject dependencies into properties of the Service class that have been annotated with @Inject.

# 2. AopManager

AopManager is used to manage various Aop configurations.

# 2.1 addMapping(...)

addMapping is used to establish a mapping relationship between an interface/abstract class and its implementation class. For instance:

AopManager.me().addMapping(Service.class, MyService.class);
1

With the mapping above, the following code will create a MyService object for Service, not a Service object:

// Here, the retrieved object is MyService
Aop.get(Service.class);
 
// Here, the injected object is MyService
@Inject
Service service;
1
2
3
4
5
6

The purpose of AopManager.me().addMapping(...) is to specify the concrete implementation class to be injected for an interface or abstract class.

# 2.2 addSingletonObject(...)

Since Aop does not support passing parameters to constructors when creating objects, addSingletonObject(...) is provided to add singleton objects:

// The Service class constructor has two parameters passed in
Service service = new Service(paraAaa, paraBbb);
AopManager.me().addSingletonObject(service);
1
2
3

After completing the above code, you can retrieve the singleton object anywhere as follows:

// Retrieving uses the singleton object
service = Aop.get(Service.class);
 
// Injection can also use the previously configured singleton object
@Inject
Service service;
1
2
3
4
5
6

Before adding as a singleton object, dependencies can be injected into it:

Service service = new Service(paraAaa, paraBbb);
// Here, dependencies are injected into Service
Aop.inject(service);
 
// After injecting dependencies for the singleton, add it as a singleton for subsequent use
AopManager.me().addSingletonObject(service);
1
2
3
4
5
6

# 2.3 setAopFactory(...)

setAopFactory(...) allows users to extend the AopFactory implementation class to implement more extensible features. For example, the jboot project extension for injecting remote access objects: https://gitee.com/JbootProjects/jboot/blob/master/src/main/java/io/jboot/aop/JbootAopFactory.java. In JbootAopFactory.java, doInjectRPC implements remote procedure call injection.

Last Updated: 9/17/2023, 6:05:19 AM